# -*- coding: utf-8 -*-
# This file has been generated, if you wish to
# modify it in a permanent way, please refer
# to the script file : gen/generator_python.rb

from api import *
from random import *

# Fonction appelée au début de la partie.
def partie_init():
    pass

# Fonction appelée à chaque tour.
def jouer_tour():
    
    for i in liste_portails():
        if portail_joueur(i)==moi():
            lier(i)
    (x,y)=position_agent(moi())
    lp=liste_portails ()
    p=(lp[0])
    vmin=10000
    for i in lp:
        pj=portail_joueur(i)
        if (pj==-1 or (pj==adversaire() and len(liens_incidents_portail(i))>=3)) and (i!=position_agent(adversaire())):
            x1,y1=i
                
            ll=[]
            val=0 
            for j in liste_portails():
                if portail_joueur(j)==moi() and liens_bloquants(j,i)==[]:
                    ll=ll+[j]
            if len(ll)>=1:i1=ll[0]
            if len(ll)>=2:j1=ll[1]
            if len(ll)>=2:
                for k in ll:
                    for j in ll:
                        v=score_triangle(k,j,i)
                        if lien_existe(k,j) and v>val:
                            val=v
                            i1=k
                            j1=j
            ll=liste_liens()
            boo2=True
           
            advance=12
            if tour_actuel()<40:advance=8
            if tour_actuel()<16:advance=4
            
            
            for j in ll:
                if intersection_segments((x,y),i,j.extr1,j.extr2) and j.joueur_l:boo2=False
            v=abs(x1-x)+abs(y1-y)-len(liens_incidents_portail(i))**2-val//advance
            
            if pj==adversaire():v-=4
            
            for a in liste_portails():
                if portail_joueur(a)==moi():
                    for b in liste_portails():
                        if portail_joueur(b)==moi():
                            for c in liste_portails():
                                if portail_joueur(c)==moi():
                                    if point_dans_triangle((x,y),a,b,c) and pj==-1:v+=20
            
            if boo2:v-=5
            if v<vmin:
                vmin=v
                p=i
    x1,y1=p
    boo=True
    while points_deplacement()>0 and boo:
        (x,y)=position_agent(moi())
        if x1>x:deplacer((x+1,y))
        elif x1<x:deplacer((x-1,y))
        elif y1>y:deplacer((x,y+1))
        elif y1<y:deplacer((x,y-1))
        else:boo=False
        if points_deplacement()==0:utiliser_turbo()
            
    if portail_joueur(position_agent(moi()))!=-2:
        neutraliser()
        capturer()
        if 1==1:
            ll=[]
            for i in liste_portails():
                if portail_joueur(i)==moi() and liens_bloquants(i,position_agent(moi()))==[]:
                    ll=ll+[i]
            
            if len(ll)>=1:i1=ll[0]  
            if len(ll)>=2:j1=ll[1]
            if len(ll)>=2:
                val=0
                for i in ll:
                    for j in ll:
                        v=score_triangle(i,j,position_agent(moi()))
                        if lien_existe(i,j) and v>val:
                            val=v
                            i1=i
                            j1=j
            for i in liste_portails():
                if portail_joueur(i)==moi():
                    lier(i)
            ajouter_bouclier()
            #ajouter_bouclier()
    
    
    
    
    for i in liste_portails():
        if portail_joueur(i)==moi():
            lier(i)
    (x,y)=position_agent(moi())
    lp=liste_portails ()
    p=(lp[0])
    vmin=10000
    for i in lp:
        pj=portail_joueur(i)
        if (pj==-1 or (pj==adversaire() and len(liens_incidents_portail(i))>=3)) and (i!=position_agent(adversaire())):
            x1,y1=i
                
            ll=[]
            val=0 
            for j in liste_portails():
                if portail_joueur(j)==moi() and liens_bloquants(j,i)==[]:
                    ll=ll+[j]
            if len(ll)>=1:i1=ll[0]
            if len(ll)>=2:j1=ll[1]
            if len(ll)>=2:
                for k in ll:
                    for j in ll:
                        v=score_triangle(k,j,i)
                        if lien_existe(k,j) and v>val:
                            val=v
                            i1=k
                            j1=j
            ll=liste_liens()
            boo2=True
           
            advance=12
            if tour_actuel()<40:advance=8
            if tour_actuel()<16:advance=4
            
            
            for j in ll:
                if intersection_segments((x,y),i,j.extr1,j.extr2) and j.joueur_l:boo2=False
            v=abs(x1-x)+abs(y1-y)-len(liens_incidents_portail(i))**2-val//advance
            
            if pj==adversaire():v-=4
            
            for a in liste_portails():
                if portail_joueur(a)==moi():
                    for b in liste_portails():
                        if portail_joueur(b)==moi():
                            for c in liste_portails():
                                if portail_joueur(c)==moi():
                                    if point_dans_triangle((x,y),a,b,c) and pj==-1:v+=20
            
            if boo2:v-=5
            if v<vmin:
                vmin=v
                p=i
    x1,y1=p
    boo=True
    while points_deplacement()>0 and boo:
        (x,y)=position_agent(moi())
        if x1>x:deplacer((x+1,y))
        elif x1<x:deplacer((x-1,y))
        elif y1>y:deplacer((x,y+1))
        elif y1<y:deplacer((x,y-1))
        else:boo=False
        if points_deplacement()==0:utiliser_turbo()
            
    if portail_joueur(position_agent(moi()))!=-2:
        neutraliser()
        capturer()
        if 1==1:
            ll=[]
            for i in liste_portails():
                if portail_joueur(i)==moi() and liens_bloquants(i,position_agent(moi()))==[]:
                    ll=ll+[i]
            
            if len(ll)>=1:i1=ll[0]  
            if len(ll)>=2:j1=ll[1]
            if len(ll)>=2:
                val=0
                for i in ll:
                    for j in ll:
                        v=score_triangle(i,j,position_agent(moi()))
                        if lien_existe(i,j) and v>val:
                            val=v
                            i1=i
                            j1=j
            for i in liste_portails():
                if portail_joueur(i)==moi():
                    lier(i)
            ajouter_bouclier()
            ajouter_bouclier()


# Fonction appelée à la fin de la partie.
def partie_fin():
    pass # Place ton code ici


